Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

343
Vistas
Why use the "use" keyword in PHP to import core identifiers?

Sometimes in some libraries like thephpleague/csv, I see the use keyword to import core identifiers (e.g. functions, classes or constants) but I can't understand why they are being imported at all.

See the following example:

use Generator;
use SplFileObject;
use function filter_var;
use function get_class;
use function mb_strlen;
use function rawurlencode;
use function sprintf;
use function str_replace;
use function str_split;
use function strcspn;
use function strlen;
use const FILTER_FLAG_STRIP_HIGH;
use const FILTER_FLAG_STRIP_LOW;
use const FILTER_SANITIZE_STRING;

Or more from here.

These classes, functions and constants that are imported in this file are belong to PHP core, so we wouldn't actually need to import them.

Why this and other libraries import these?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

When you're in a namespace, every bare name refers to a name within that namespace. I.e.:

namespace Foo;

echo bar();

bar here really means Foo\bar.

There are rules for fallbacks to global names if the name doesn't exist within this namespace. If you don't want to rely on those rules, or if you do actually have those names defined in your namespace and you don't want to have to constantly write \bar() explicitly, that's when you may explicitly use those global names to make it unambiguous that bar refers to \bar and not Foo\bar.

It's probably not necessary most of the time for PHP builtin names, but perhaps the project author has simply established that as a rule to avoid bugs due to namespace resolution ambiguity.

over 4 years ago · Santiago Trujillo Denunciar

0

The objective with this is to perform a (micro) performance optimization, as described here. (Archived link, just in case).

The idea being that by using avoiding the global name lookup and skipping the fall-back rules, some performance can be gained.

Note that with any real-life workload these gains are negligible and inconsequential, and they are mostly made irrelevant by opcode caching. Nevertheless, the performance gain, minute as it may be, exists.

You can read some lengthy and interesting discussion about this:

  • Reddit discussing the article linked above
  • Symfony project deciding not to apply this optimization
  • Readme file for a script that performs this optimization automatically
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda